<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:ddl="livcos.org/ns/db/ddl"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:sd="livcos.org/ns/design/system"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="c doc ddl pm sd f v xs">
  
  <xsl:import href="base.xsl"/>
  
	<xsl:template match="ddl:view" mode="ddl_content">
		<table class="table_overview">
			<thead>
				<tr>
					<th>Feld</th>
					<th>Type</th>
					<th>Dimension</th>
				</tr>
			</thead>
			<tbody>
				<xsl:apply-templates select="ddl:field" mode="ddl_view_overview">
					<xsl:sort select="lower-case(@name)"/>
				</xsl:apply-templates>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="3" class="table_legend">
						<span class="mandatory">&#160;&#160;</span>
						<xsl:text> Mandatory&#160;&#160; </xsl:text>
						<span class="nullable">&#160;&#160;</span>
						<xsl:text> Optional (allow NULL)</xsl:text>
					</td>
				</tr>
			</tfoot>
		</table>
		<xsl:apply-templates select="ddl:dependencies"/>
		<xsl:call-template name="ddl_doc"/>
		<v:chapter>
			<v:title>Felder</v:title>
			<xsl:apply-templates select="ddl:field" mode="ddl_view">
				<xsl:sort select="lower-case(@name)"/>
			</xsl:apply-templates>
		</v:chapter>
		<v:chapter>
			<v:title>Definition</v:title>
			<xsl:apply-templates select="ddl:def"/>
		</v:chapter>
		<xsl:call-template name="ddl_constraints"/>
  </xsl:template>
  
  <xsl:template match="ddl:field" mode="ddl_view_overview">
    <tr>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="@nullable">nullable</xsl:when>
          <xsl:otherwise>mandatory</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <td>
        <a href="#{@id}"><xsl:value-of select="@name"/></a>
      </td>
      <td align="center">
        <xsl:value-of select="@dbtype" />
      </td>
      <td align="center">
      	<xsl:apply-templates select="@size"/>
      	<xsl:if test="not(@size)">&#160;</xsl:if>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="ddl:field" mode="ddl_view">
  	<v:chapter id="{@id}">
  		<v:title>
  			<xsl:call-template name="ddl-search">
  				<xsl:with-param name="label" select="string(@name)"/>
  				<xsl:with-param name="query" select="concat(@name, ' AND ', ../@name)"/>
  			</xsl:call-template>
  		</v:title>
    	<xsl:apply-templates select="*"/>
    	<p class="block">
    		<xsl:text>Das </xsl:text>
    		<xsl:if test="@nullable">optionale</xsl:if> Feld '<xsl:value-of select="@name"/>
    		<xsl:text>' vom Typ </xsl:text>
    		<xsl:apply-templates select="." mode="ddl_type"/>
      </p>
    </v:chapter>
  </xsl:template>

	<xsl:template match="ddl:dependencies">
		<v:chapter>
			<v:title>Abhängig von Tabellen</v:title>
			<ul>
				<xsl:for-each select="ddl:table">
					<li>
						<xsl:variable name="objName" select="f:encode-for-tagname(@ref-name)"/>
						<xsl:variable name="view" select="f:resolve-object-id($objName, .)"/>
						<xsl:variable name="func" select="f:resolve-object-id(concat('../function/', $objName), .)"/>
						<xsl:variable name="table" select="f:resolve-object-id(concat('../table/', $objName), .)"/>
						<xsl:variable name="objId">
							<xsl:choose>
								<xsl:when test="f:object-available($view)"><xsl:value-of select="$view"/></xsl:when>
								<xsl:when test="f:object-available($func)"><xsl:value-of select="$func"/></xsl:when>
								<xsl:otherwise><xsl:value-of select="$table"/></xsl:otherwise>
							</xsl:choose>
						</xsl:variable>
						<a href="{f:caller-uri($viewObjId, $objId)}.html">
							<xsl:value-of select="@ref-name"/>
						</a>
					</li>
				</xsl:for-each>
			</ul>
		</v:chapter>
	</xsl:template>
	
</xsl:stylesheet>
